package dy.app.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.Md5Crypt;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * 加密相关工具类
 * Author: cKnight
 * Date: 2024/6/11
 */
@Slf4j
public class EncryptUtils {
    /**
     * 密码md5加密
     * @param pwd
     * @param secret
     * @return
     */
    public static String passwordEncrypt(String pwd, String secret) {
        return Md5Crypt.md5Crypt(pwd.getBytes(StandardCharsets.UTF_8),secret);
    }

    /**
     * MD5加密
     *
     * @param data
     * @return
     */
    public static String MD5(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(data.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte item : array) {
                sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
            }

            return sb.toString().toUpperCase();
        } catch (Exception exception) {
        }
        return null;

    }
}
